<template>
<Modal
	v-model="show"
	title="选择公司主体"
	:width="840"
	>
	<div>
		<Table :columns="Columns" :data="list" style="width: 100%;" border ref="table" @on-selection-change="sel_change"></Table>
		<div style="font-size: 14px;overflow: hidden;padding: 10px;">
			
			<div style="float:right;">
				<Page :total="count" show-elevator show-total @on-change="goto_page"  />
			</div>
		</div>
	</div>

	<div slot="footer" style="text-align: center;">
		<Button type="success" size="large" @click="submit">选中公司主体</Button>
	</div>
</Modal>
</template>

<script>

export default {
	name: 'company_box',
	data(){
		return {
			show:false,
			Columns:[
				{
					type: 'selection',
					width: 80,
					align: 'center'
				},
				{
					title: '公司主体全称',
					key: 'name',
					align: 'center'
				},
				{
					title: '简称',
					key: 'sx_name',
					width: 160,
					align: 'center'
				}
			],
			list:[],
			count:0,
			page:1,
			sels:{}
		};
	},
	props:['value'],
	created(){
		this.show = this.value;
		
	},
	watch:{
		value(val){
			if(val){
				this.sels = {};
				this.goto_page(1);
			}
			this.show = val;
		},
		show(val){
			this.$emit('input',val);
		},
	},
	methods: {
		load_data(){
			var self = this;
			var postdata = {
				page: this.page,
			}
			
			Whttp.load('User/Company/get_list',postdata,(res)=>{
				self.list = res.list;
				self.count = parseInt(res.count);
				
				setTimeout(function(){
					var objData = self.$refs['table'].$refs.tbody.objData;
					var sels = self.sels[self.page];
					if(!sels){
						self.sels[self.page] = [];
						return;
					}
					var ids = [];
					for(var i=0;i<sels.length;i++){
						ids.push(sels[i].id);
					}
					
					for(var i in objData){
						if(ids.in_array(objData[i].id)){
							objData[i]._isChecked=true;
						}
					}
					
				},100);
			});
		},
		goto_page(page){
			this.page = page;
			this.load_data();
		},
		submit() {
			var arr = [];
			
			for(var i in this.sels){
				for(var j=0;j<this.sels[i].length;j++){
					arr.push(this.sels[i][j]);
				}
			}
			this.$emit('change',arr);
			this.show = false;
		},
		sel_change(val){
			var ids = [];
			for(var i=0;i<val.length;i++){
				ids.push({id:val[i].id,name:val[i].sx_name});
			}
			this.sels[this.page] = ids;
		}
	}
}
</script>
<style scoped>

</style>